#include "projectmanagementpanel.h"

#include "xmlrpcconnection.h"
#include "xmlrpcrequest.h"
#include "xmlrpcresponse.h"

#include <utils/detailswidget.h>

#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
#include <QtGui/QGridLayout>
#include <QtGui/QLineEdit>
#include <QtGui/QLabel>
#include <QtCore/QCoreApplication>
#include <QtCore/QSettings>

ProjectManagementPanel::ProjectManagementPanel(SessionManager *session, Project *project)
    : m_widget(new ProjectManagementWidget(session, project))
{

}

ProjectManagementPanel::~ProjectManagementPanel()
{
    delete m_widget;
}

QString ProjectManagementPanel::displayName() const
{
    return QCoreApplication::translate("ProjectManagementPanel", "Tickets");
}

QWidget *ProjectManagementPanel::widget() const
{
    return m_widget;
}

QIcon ProjectManagementPanel::icon() const
{
    return m_icon;
}

ProjectManagementWidget::ProjectManagementWidget(SessionManager *session, Project *project,
                   QWidget *parent)
    : QWidget(parent), m_session(session), m_project(project), m_settings("Nokia", "Tickets Plugin")
{
    QVBoxLayout *vbox = new QVBoxLayout(this);
    vbox->setContentsMargins(0, 0, 0, 0);
    m_detailsContainer = new Utils::DetailsWidget(this);
    m_detailsContainer->setState(Utils::DetailsWidget::NoSummary);
    vbox->addWidget(m_detailsContainer);

    QWidget *detailsWidget = new QWidget(m_detailsContainer);
    m_detailsContainer->setWidget(detailsWidget);
    QGridLayout *layout = new QGridLayout(detailsWidget);

    layout->setContentsMargins(0, -1, 0, -1);

    m_settings.beginGroup(project->displayName());
    QLabel *userLabel = new QLabel(tr("Username:"), this);
    m_username = new QLineEdit(m_settings.value("Username", "").toString(), this);
    QLabel *passwordLabel = new QLabel(tr("Password:"), this);
    m_password = new QLineEdit(m_settings.value("Password", "").toString(), this);
    m_password->setEchoMode(QLineEdit::Password);
    QLabel *urlLabel = new QLabel(tr("XML-RPC URL:"), this);
    m_url = new QLineEdit(m_settings.value("Url", "").toString(), this);
    m_settings.endGroup();

    layout->addWidget(userLabel);
    layout->addWidget(m_username, 0, 1);
    layout->addWidget(passwordLabel);
    layout->addWidget(m_password, 1, 1);
    layout->addWidget(urlLabel);
    layout->addWidget(m_url, 2, 1);

    connect(m_username, SIGNAL(editingFinished()), this, SLOT(save()));
    connect(m_password, SIGNAL(editingFinished()), this, SLOT(save()));
    connect(m_url, SIGNAL(editingFinished()), this, SLOT(save()));
}

void ProjectManagementWidget::save()
{
    m_settings.beginGroup(m_project->displayName());
    m_settings.setValue("Username", m_username->text());
    m_settings.setValue("Password", m_password->text());
    m_settings.setValue("Url", m_url->text());
    m_settings.setValue("ID", "trac");
    m_settings.endGroup();
}

ProjectManagementPanelFactory::ProjectManagementPanelFactory(SessionManager *session)
    : m_session(session)
{

}

QString ProjectManagementPanelFactory::id() const
{
    return QLatin1String(MANAGEMENT_PANEL_ID);
}

QString ProjectManagementPanelFactory::displayName() const
{
    return QCoreApplication::translate("ProjectManagementPanelFactory", "Project Management");
}

bool ProjectManagementPanelFactory::supports(Project *project)
{
    Q_UNUSED(project);
    return true;
}

IPropertiesPanel *ProjectManagementPanelFactory::createPanel(Project *project)
{
    return new ProjectManagementPanel(m_session, project);
}

TrackTicketSetup::TrackTicketSetup()
{
}

TrackTicketSetup::~TrackTicketSetup()
{
}

